package cn.felord.debezium.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

/**
 * LED大屏推送hex16bite特别转不同于  合金称和报警灯
 */
@Slf4j
public class TcpClientPusDuan {
    public static void push(String ip,String port,String hi,int cut) {
         System.out.println("推送去空格数据为：" + hi.replaceAll(" ",""));
//         System.out.println("推送转中文:"+hexToCHinise(hexStringToByteArray(hi.replaceAll(" ",""))));
        Socket socket = null;
        try {
            socket = new Socket(ip, Integer.valueOf(port));
            OutputStream outputStream = null;  // Output stream to send data to the server
            outputStream = socket.getOutputStream();
            //d第二种方式
            byte[] data = new byte[0];
            try {
                data = Hex.decodeHex(hi.replaceAll(" ","").toCharArray());
            } catch (DecoderException e) {
                System.out.println(e);
                e.printStackTrace();
            }
                outputStream.write(data);
                outputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String sss = hi.replaceAll(" ","");
            parseStr(sss,cut);//加日志打印
        }
    public static void push(String ip,String port,String hi,String cutStr) {
         System.out.println("推送去空格数据为：" + hi.replaceAll(" ",""));
//         System.out.println("推送转中文:"+hexToCHinise(hexStringToByteArray(hi.replaceAll(" ",""))));
        Socket socket = null;
        try {
            socket = new Socket(ip, Integer.valueOf(port));
            OutputStream outputStream = null;  // Output stream to send data to the server
            outputStream = socket.getOutputStream();
            //d第二种方式
            byte[] data = new byte[0];
            try {
                data = Hex.decodeHex(hi.replaceAll(" ","").toCharArray());
            } catch (DecoderException e) {
                System.out.println(e);
                e.printStackTrace();
            }
                outputStream.write(data);
                outputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String sss = hi.replaceAll(" ","");
            parseStr(sss,cutStr.replaceAll(" ","").length());//加日志打印
        }
    public static void push(String ip,String port,String hi,boolean parse) {
         System.out.println("推送去空格数据为：" + hi.replaceAll(" ",""));
//         System.out.println("推送转中文:"+hexToCHinise(hexStringToByteArray(hi.replaceAll(" ",""))));
        Socket socket = null;
        try {
            socket = new Socket(ip, Integer.valueOf(port));
            OutputStream outputStream = null;  // Output stream to send data to the server
            outputStream = socket.getOutputStream();
            //d第二种方式
            byte[] data = new byte[0];
            try {
                data = Hex.decodeHex(hi.replaceAll(" ","").toCharArray());
            } catch (DecoderException e) {
                System.out.println(e);
                e.printStackTrace();
            }
                outputStream.write(data);
                outputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(parse){
                String sss = hi.replaceAll(" ","");
                parseStr(sss,26);//加日志打印
            }
        }
        //16进制 转回 10
    public static void parseStr(String s,int cut){
        System.out.println("解析：" + s);
        s = s.substring(cut);//去头
        int l =  s.length()/4;
        for (int i = 0; i < l; i++) {
            String substring = s.substring(0, 4);
            System.out.println(substring);
            int i10 = NumberUtils.hex16to10(substring);
            System.out.println(i10);
            s = s.substring(4);//去4位头
        }
    }
    // 将16进制字符串转换为字节数组
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    public static void main(String[] args) throws DecoderException, IOException {

        parseStr("00010000001B0110010400070E0000286400000000000000000000",
                "00 01 00 00 00 1B 01 10 00 FA 00 07 0E".replaceAll(" ","").length());


//        //diyiz第一种方式
//        Socket socket = null;
//        socket = new Socket("10.20.20.9", 5004);
//        OutputStream outputStream = null;  // Output stream to send data to the server
//        outputStream = socket.getOutputStream();
////        outputStream.write("474732304843".getBytes());//🚶‍🚶‍这种不行
////        outputStream.write(hexStringToByteArray("47 47 32 30 48 43".replaceAll(" ","")));
//        //d第二种方式
//        byte[] data = new byte[0];
//        try {
//            data = Hex.decodeHex("47 47 32 30 48 43".replaceAll(" ","").toCharArray());
//        } catch (DecoderException e) {
//            System.out.println(e);
//            e.printStackTrace();
//        }
//        outputStream.write(data);
//        outputStream.close();
//        socket.close();


    }

}
